package bookManageSystem.view;

import bookManageSystem.Mat.StaticMapExample;
import bookManageSystem.po.Doctor;
import bookManageSystem.po.Employee;
import bookManageSystem.po.Order;
import bookManageSystem.service.DoctorService;
import bookManageSystem.service.EmployeeService;
import bookManageSystem.service.OrderService;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

public class DepartmentSelectPanel extends JPanel {

    private DoctorService doctorService = new DoctorService();
    private EmployeeService employeeService = new EmployeeService();
    private OrderService orderService = new OrderService();
    private DefaultTableModel tableModel; // 表格模型
    private JTable employeeTable; // 员工信息表格

    public DepartmentSelectPanel() {
        // 设置面板布局
        setLayout(new BorderLayout());

        // 创建科室选择标签和下拉菜单
        JLabel departmentLabel = new JLabel("选择科室:");
        String[] departments = {"内科", "外科", "儿科", "妇产科", "五官科"}; // 示例科室
        JComboBox<String> departmentComboBox = new JComboBox<>(departments);

        // 创建确定按钮
        JButton confirmButton = new JButton("确定");
        confirmButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String selectedDepartment = (String) departmentComboBox.getSelectedItem();
                System.out.println("选择的科室: " + selectedDepartment);
                // 初始化表格并加载员工数据
                initializeTable();
                loadEmployees(selectedDepartment);
            }
        });

        // 创建预约按钮
        JButton bookButton = new JButton("预约");
        bookButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int selectedRow = employeeTable.getSelectedRow();
                if (selectedRow != -1) {
                    Doctor selectedDoctor = getSelectedDoctor(selectedRow);
                    showCustomButtonDialog(selectedDoctor);
                } else {
                    JOptionPane.showMessageDialog(DepartmentSelectPanel.this, "请先选择一个医生！", "提示", JOptionPane.WARNING_MESSAGE);
                }
            }
        });

        // 将标签、下拉菜单和按钮添加到同一个面板
        JPanel topPanel = new JPanel();
        topPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
        topPanel.add(departmentLabel);
        topPanel.add(departmentComboBox);
        topPanel.add(confirmButton);
        topPanel.add(bookButton); // 添加预约按钮

        // 将组件添加到主面板
        add(topPanel, BorderLayout.NORTH);

        // 初始化表格（可以在构造函数中初始化一次，或者在第一次选择时）
        initializeTable();
        add(new JScrollPane(employeeTable), BorderLayout.CENTER);
    }

    private void initializeTable() {
        // 仅初始化一次表格结构
        if (tableModel == null) {
            tableModel = new DefaultTableModel(new String[]{"医生编码", "姓名", "性别", "年龄", "职位", "介绍", "看病时间", "科室"}, 0);
            employeeTable = new JTable(tableModel);
        }
    }

    // 加载员工列表到表格
    private void loadEmployees(String selectedDepartment) {
        List<Doctor> list = doctorService.selectDoctoByDepartment(selectedDepartment);

        tableModel.setRowCount(0); // 清空表格
        for (Doctor doctor : list) {
            tableModel.addRow(new Object[]{doctor.getDoctorId(), doctor.getName(), doctor.getGender(),
                    doctor.getAge(), doctor.getPosition(), doctor.getDescription(), doctor.getSchedule(), doctor.getDepartment()});
        }
    }

    // 获取选中的医生信息
    private Doctor getSelectedDoctor(int selectedRow) {
        Object valueAt = tableModel.getValueAt(selectedRow, 0);
        int doctorId = (Integer) valueAt;
        String name = (String) tableModel.getValueAt(selectedRow, 1);
        String gender = (String) tableModel.getValueAt(selectedRow, 2);
        int age = (int) tableModel.getValueAt(selectedRow, 3);
        String position = (String) tableModel.getValueAt(selectedRow, 4);
        String description = (String) tableModel.getValueAt(selectedRow, 5);
        String schedule = (String) tableModel.getValueAt(selectedRow, 6);
        String department = (String) tableModel.getValueAt(selectedRow, 7);

        // 假设有一个Doctor构造函数可以接受这些参数
        return new Doctor(doctorId, name, gender, age, position, description, schedule, department);
    }

    // 弹出预约弹框
    private void showCustomButtonDialog(Doctor doctor) {

        String message = "您已选择：\n" +
                "姓名: " + doctor.getName() + "\n" +
                "科室: " + doctor.getDepartment() + "\n" +
                "看病时间: " + doctor.getSchedule() + "\n" +
                "挂号费: 6元";

        // 自定义按钮
        Object[] options = {"支付", "取消"};

        // 显示带有自定义按钮的对话框
        int option = JOptionPane.showOptionDialog(DepartmentSelectPanel.this,
                message,
                "支付信息",
                JOptionPane.YES_NO_OPTION,
                JOptionPane.INFORMATION_MESSAGE,
                null,
                options,
                options[0]); // 默认选择第一个按钮

        if (option == JOptionPane.YES_OPTION) {
            //修改用户
            String userId = StaticMapExample.get("userId");
            Employee employeeById = employeeService.getEmployeeById(Integer.parseInt(userId));
            if (0 > employeeById.getBalance() - 6) {
                JOptionPane.showMessageDialog(DepartmentSelectPanel.this, "费用不够", "消息提醒", JOptionPane.INFORMATION_MESSAGE);
                return;
            }
            Boolean b = employeeService.updateEmployeeByuserId(Integer.parseInt(userId));
            if (!b) {
                JOptionPane.showMessageDialog(DepartmentSelectPanel.this, "支付失败", "消息提醒", JOptionPane.INFORMATION_MESSAGE);

            }
            Order order = new Order();
            order.setName(employeeById.getUsername());
            order.setCost(6);
            order.setDocke(doctor.getName());
            order.setTime(doctor.getSchedule());
            boolean i = orderService.addOrder(order);
            if (!i) {
                JOptionPane.showMessageDialog(DepartmentSelectPanel.this, "支付失败", "消息提醒", JOptionPane.INFORMATION_MESSAGE);

            } else {
                JOptionPane.showMessageDialog(DepartmentSelectPanel.this, "支付成功", "消息提醒", JOptionPane.INFORMATION_MESSAGE);
            }

            System.out.println("用户选择了支付");
        } else {
            System.out.println("用户取消了支付");
        }
    }
}